var arr = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16]
];
function printCircle(arr, columns,rows, start) {
    var endX = columns - 1 -start;
    var endY = rows - 1 - start;
    for(var i = start;i <= endX;i ++){
        console.log(arr[start][i]);
    }
    if(start < endY){
        for(var i = start + 1;i <= endY;i ++) {
            console.log(arr[i][endX]);
        }
    }
    if(start < endX && start < endY){
        for(var i = endX-1;i >= start;i --) {
            console.log(arr[endY][i]);
        }
    }
    if(start < endY - 1 && start < endX){
        for(var i = endY-1;i >= start + 1;i--) {
            console.log(arr[i][start])
        }
    }
}
function printMatrix(newArr,columns,rows){
    if(newArr == null || columns <= 0 || rows<= 0)
        return;
    var start = 0;
    while(columns > start * 2 && rows > start * 2){
        printCircle(newArr, columns, rows, start);
        ++start;
    }
}
printMatrix(arr,arr.length,arr.length);


